package com.rzjm.exercise.module.controller;

import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.Parameter;
import com.rzjm.exercise.module.domain.Survey;
import com.rzjm.exercise.module.service.ParameterService;
import com.rzjm.exercise.module.service.SurveyService;
import com.rzjm.exercise.sys.system.user.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 问卷列表
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2021-11-5 12:36:23
 */
@Controller
@RequestMapping("/module/survey")
public class SurveyController {
    @Resource
    private SurveyService surveyService;
    @Resource
    private UserService userService;
    @Resource
    private ParameterService parameterService;

    @Log("查询设备页面")
    @GetMapping()
    @RequiresPermissions("module:survey:survey")
    String survey() {
        return "module/survey/survey";
    }

    @Log("查询设备页面")
    @GetMapping("/measure")
    @RequiresPermissions("module:survey:survey")
    String measure(Model model) {
        Parameter parameter = parameterService.findById(1);
        model.addAttribute("parameter",parameter);
        return "module/survey/measure";
    }

    @Log("查询设备")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("module:survey:survey")
    public DataTableLayUI<Survey> list(DataTableLayUI<Survey> condition) {
        DataTableLayUI<Survey> dt = surveyService.pageSearchLayUI(condition);
        return dt;
    }


    @Log("新增设备页面")
    @GetMapping("/add")
    @RequiresPermissions("module:survey:add")
    String add(Model model) {
        Survey survey = new Survey();
        model.addAttribute("survey", survey);
        return "module/survey/detail";
    }


    @Log("编辑设备页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:survey:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        Survey survey = surveyService.findById(id);
        model.addAttribute("survey", survey);
        return "module/survey/detail";
    }

    /**
     * 保存
     */
    @Log("新增设备")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:survey:add")
    public R save(@Valid Survey survey) {
        if (surveyService.save(survey)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("更新设备")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:survey:edit")
    public R update(@Valid Survey survey) {
        surveyService.updateById(survey);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除设备")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("module:survey:remove")
    public R remove( Integer id) {
        if (surveyService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }
}
